package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.NewcomerWelfareAdapter;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.NewcomerItem;
import com.yjhb.android.sbz.entity.NewcomerState;
import com.yjhb.android.sbz.entity.UserBehavior;
import com.yjhb.android.sbz.mvp.newcomer.NewcomerWelfarePresenter;
import com.yjhb.android.sbz.mvp.newcomer.NewcomerWelfareView;
import com.yjhb.android.sbz.tool.ShareTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxNetTool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.iwgang.countdownview.CountdownView;

import static com.yjhb.android.sbz.tool.EventMsg.REQUEST_HALL_SWITCH_HIGH;
import static com.yjhb.android.sbz.tool.EventMsg.REQUEST_HALL_SWITCH_LOW;
import static com.yjhb.android.sbz.tool.EventMsg.REQUEST_MAIN_SWITCH_FRAGMENT;

/**
 * 创建者 zx
 * 创建时间 2020/12/28 10:54
 */
public class NewcomerWelfareActivity extends BaseActivity<NewcomerWelfarePresenter> implements NewcomerWelfareView, BaseRecyclerAdapter.ItemClickListener<NewcomerItem> {
    private final String TAG = "NewcomerWelfareActivity";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.bottom)
    Button bottom;
    @BindView(R.id.countdownView)
    CountdownView countdownView;

    NewcomerWelfareAdapter adapter;
    List<NewcomerItem> newcomerItems = new ArrayList<>();


    @Override
    protected NewcomerWelfarePresenter initPresenter() {
        return new NewcomerWelfarePresenter(this);
    }

    @Override
    protected boolean isCheckLogin() {
        return true;
    }

    @Override
    protected boolean isImmerse() {
        return false;
    }

    @Override
    protected int getStatusBarResource() {
        return R.color.yellow_100;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_newcomer_welfare;
    }

    @Override
    protected void initUI() {
        initNewcomerItems();
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter = new NewcomerWelfareAdapter(this));
        adapter.setData(newcomerItems);
        downTime();
    }

    @Override
    protected void initData() {
        presenter.getNewcomerState();
    }

    private void updateState(NewcomerState state) {
        newcomerItems.get(0).setState(state.getTaskOne());
        newcomerItems.get(1).setState(state.getTaskFive());
        newcomerItems.get(2).setState(state.getTaskTwo());
        newcomerItems.get(3).setState(state.getTaskThree());
        newcomerItems.get(4).setState(state.getTaskSix());
        newcomerItems.get(5).setState(state.getTaskFour());
        adapter.notifyDataSetChanged();
    }

    private void initNewcomerItems() {
        newcomerItems.clear();
        NewcomerItem item = new NewcomerItem();
        item.setNumber(1);
        item.setTitle("完成新手指南");
        item.setContent("新人福利无门槛，提现实时到账");
        item.setMoney("0.1");
        item.setState(0);
        newcomerItems.add(item);

        item = new NewcomerItem();
        item.setNumber(2);
        item.setTitle("完成简单答题");
        item.setContent("完成简单答题，领取红包");
        item.setMoney("0.1");
        item.setState(0);
        newcomerItems.add(item);

        item = new NewcomerItem();
        item.setNumber(3);
        item.setTitle("完成1次朋友圈分享");
        item.setContent("完成1次朋友圈分享，领取红包");
        item.setMoney("0.1");
        item.setState(0);
        newcomerItems.add(item);

        item = new NewcomerItem();
        item.setNumber(4);
        item.setTitle("完成2个任务");
        item.setContent("完成2个任务（0/2）");
        item.setMoney("1");
        item.setState(0);
        newcomerItems.add(item);

        item = new NewcomerItem();
        item.setNumber(5);
        item.setTitle("再完成3个任务");
        item.setContent("再完成3个金额不小于1元的任务（0/3）");
        item.setMoney("1");
        item.setState(0);
        newcomerItems.add(item);

        item = new NewcomerItem();
        item.setNumber(6);
        item.setTitle("完成1次提现");
        item.setContent("完成1次提现");
        item.setMoney("0.2");
        item.setState(0);
        newcomerItems.add(item);
    }

    private void downTime() {
        Date createTime = App.userInfo.getCreateTime();
        long targetTime = createTime.getTime() + 1000 * 60 * 60 * 24 * 7;
        long surplus = targetTime - System.currentTimeMillis();
        countdownView.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() {
            @Override
            public void onEnd(CountdownView cv) {
                adapter.setDowntimeEnd(true);
                adapter.notifyDataSetChanged();
            }
        });
        if (surplus > 0) {
            countdownView.start(surplus);
        } else {
            countdownView.setVisibility(View.GONE);
            adapter.setDowntimeEnd(true);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void regEvent() {
        adapter.setOnItemClick(this);
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick(R.id.bottom)
    void bottom() {
        ZxActivityTool.skipActivity(this, EverydayWelfareActivity.class);
    }

    @Override
    public void onWait(String msg) {
        loadingDialog.setTipsText(msg);
        loadingDialog.show();
    }

    @Override
    public void onState(NewcomerState state) {
        loadingDialog.cancel();
        presenter.getUserBehavior();
        if (state != null) {
            updateState(state);
        } else {
            ZxToast.showINFOBottomToast("获取失败");
        }
    }

    @Override
    public void onSubmitSuccess() {
        loadingDialog.cancel();
        presenter.getNewcomerState();
        ZxToast.showSuccessBottomToast("领取成功,赶紧去查看哦~");
    }

//    @Override
//    public void onTaskCount(int count) {
////        newcomerItems.get(2).setContent("完成3个悬赏任务（" + count + "/3）");
//        adapter.notifyDataSetChanged();
//    }

    @Override
    public void UserBehavior(UserBehavior userBehavior) {
        if (userBehavior != null) {
            int i = userBehavior.getWxSpaceShare();
            if (i > 0 && newcomerItems.get(2).getState() != 1) {
                newcomerItems.get(2).setState(2);
            }
            i = userBehavior.getOverTaskNum();
            if (i > 2) {
                i = 2;
            }
            newcomerItems.get(3).setContent("完成2个任务（" + i + "/2）");
            if (i > 1 && newcomerItems.get(3).getState() != 1) {
                newcomerItems.get(3).setState(2);
            }
            i = userBehavior.getRewardOverOne();
            if (i > 3) {
                i = 3;
            }
            newcomerItems.get(4).setContent("再完成3个金额不小于1元的任务（" + i + "/3）");
            if (i > 2 && newcomerItems.get(4).getState() != 1) {
                newcomerItems.get(4).setState(2);
            }
            i = userBehavior.getGetMoneyNum();
            if (i > 0 && newcomerItems.get(5).getState() != 1) {
                newcomerItems.get(5).setState(2);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(int code, String msg, Integer type) {
        loadingDialog.cancel();
        if (!ZxNetTool.isNetworkAvailable(this)) {
            ZxToast.showINFOBottomToast("请检查您的网络哦~");
            return;
        }
        if (type == 5){

        }else {
            ZxToast.showINFOBottomToast(msg);
        }
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (!ZxNetTool.isNetworkAvailable(this)) {
            ZxToast.showINFOBottomToast("请检查您的网络哦~");
            return;
        }
        if (code == 670 || code == 672 || code == 673) {
//            if (clickItem == 1) {
//                Tool.SharePopup(getActivity(), rootView);
//            } else if (clickItem == 2) {
//                ZxActivityTool.skipActivity(this, NewUserTaskListActivity.class);
//            } else if (clickItem == 3) {
//                Tool.startWithdrawalActivity(this, 1);
//            }
        } else {
            ZxToast.showINFOBottomToast(msg);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //完成新手指南
            if (requestCode == 10) {
                presenter.newUserGift(1);
            }
            //完成简单答题
            else if (requestCode == 11) {
                presenter.newUserGift(5);
            }
        }
    }

    @Override
    public void onEventMsg(EventMsg msg) {
        super.onEventMsg(msg);
        if (msg.msg == EventMsg.USER_WX_RESULT) {
            presenter.getNewcomerState();
        }
    }

    @Override
    public void onItemClick(int index, NewcomerItem newcomerItem) {
        if (index == 0) {
            Intent intent = new Intent(this, MakeMoneyStrategyActivity.class);
            intent.putExtra("state", newcomerItem.getState() == 0 ? (adapter.isDowntimeEnd() ? 1 : 0) : 1);
            startActivity(intent);
        }else if (index == 1){
            Intent intent = new Intent(this, SubjectActivity.class);
            startActivityForResult(intent, 11);
        }
    }

    @Override
    public void onViewClick(View view, String msg, int index, NewcomerItem newcomerItem) {
        if (index == 0) {
//            if (newcomerItem.getState() == 1) {
            Intent intent = new Intent(this, MakeMoneyStrategyActivity.class);
            startActivityForResult(intent, 10);
//            }
        } else if (index == 1) {
            Intent intent = new Intent(this, SubjectActivity.class);
            startActivityForResult(intent, 11);
        } else if (index == 2) {
            if (newcomerItem.getState() == 2) {
                presenter.newUserGift(2);
            } else {
                ShareTool.getInstance().shareImgTextPyq(getActivity());
            }
        } else if (index == 3) {
            if (newcomerItem.getState() == 2) {
                presenter.newUserGift(3);
            } else {
                ZxActivityTool.skipActivity(getActivity(), MainActivity.class);
                EventBus.getDefault().post(new EventMsg(REQUEST_MAIN_SWITCH_FRAGMENT, 1));
                EventBus.getDefault().post(new EventMsg(REQUEST_HALL_SWITCH_LOW, null));
            }
        } else if (index == 4) {
            if (newcomerItem.getState() == 2) {
                presenter.newUserGift(6);
            } else {
                ZxActivityTool.skipActivity(getActivity(), MainActivity.class);
                EventBus.getDefault().post(new EventMsg(REQUEST_MAIN_SWITCH_FRAGMENT, 1));
                EventBus.getDefault().post(new EventMsg(REQUEST_HALL_SWITCH_HIGH, null));
            }
        } else if (index == 5) {
            if (newcomerItem.getState() == 2) {
                presenter.newUserGift(4);
            } else {
                Tool.startWithdrawalActivity(getActivity(), 1);
            }
        }

    }

}
